// @flow
import React, { Component } from 'react'
import styles from './JobListActions.css'
import { Button, Modal } from 'antd'
import universalFetch, { handleFetchError } from 'store/modules/fetch'
type Props = {
  history: Object,
  data: Object
}
class JobListActions extends Component {
  props: Props
  fillShow: Function
  apiShow: Function
  constructor (props: Props) {
    super(props)
    this.fillShow = this.fillShow.bind(this)
    this.apiShow = this.apiShow.bind(this)
  }
  fillShow () {
    const jobId = this.props.data.jobId
    let file = `${__SERVICE_API__}analysis/results?jobId=${jobId}&type=file`
    const { history } = this.props
    universalFetch(file)
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      history.push({
        pathname: '/analysis/job-file',
        state: {
          rowData:json.result
        } })
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  apiShow () {
    const jobId = this.props.data.jobId
    let file = `${__SERVICE_API__}analysis/results?jobId=${jobId}&type=interface`
    universalFetch(file)
    .then((res) => res.json())
    .then((json) => {
      if (json.code !== 200000) {
        throw new Error(JSON.stringify({ code: json.code, message: json.message }))
      }
      Modal.info({
        title: '接口内容：',
        content: JSON.stringify(json),
        okText:'关闭',
        onOk () {
          // console.log('OK')
        },
        onCancel () {
          // console.log('Cancel')
        }
      })
      // notification.open({
      //   message: ' ',
      //   description: JSON.stringify(json),
      //   duration: null
      // })
    })
    .catch((error) => {
      handleFetchError(error)
      console.error(error)
    })
  }
  render () {
    return (
      <div className={styles['actions']}>
        <Button
          size='small'
          type='primary'
          icon='file'
          style={{ marginRight: '2px' }}
          onClick={this.fillShow}
          >
          文件
        </Button>
        <Button
          size='small'
          type='primary'
          style={{ marginRight: '10px' }}
          onClick={this.apiShow}
          >
          接口
        </Button>
      </div>
    )
  }
}

export default JobListActions
